import api from '../../api';
import { message } from 'antd';
import { calcRange } from '../../../src/routes/performanceAnalysis/attributionAnalysis/Campisi/utils';
const index = (e) => {
  switch (e) {
    case '0':
      return 'Brinson';
    case '1':
      return 'Campisi';
    case '2':
      return 'Barra';
  }
};

export default {
  namespace: 'attributionAnalysisM',

  state: {
    MAAT_PRODUCT_INVE_TYPE: [], //投资类型字典
    modelData: [], // 字典值
    Income: undefined, // 收益分解数据
    ExcessTrendData: { xAxis: undefined, series: undefined }, // 超额趋势数据
    diffData: { series: undefined, series2: undefined, yAxis: undefined }, // 行业权重差&&行业贡献差
    info: { columns: [], dataSource: [] }, // 归因明细
    BondType: { xAxis: undefined, series: undefined }, // 按修正久期
    RevisionPeriod: { xAxis: undefined, series: undefined }, // 按债券类别
    FactorYield: { xAxis: undefined, series: undefined }, // 因子收益率
    FactorContributionDetail: { data1: {}, data2: {} }, //因子收益贡献
  },
  effects: {
    // 获取收益分解
    *getIncome({ payload }, { call, put }) {
      const test = index(payload.setupModel); // 根据不同模型调不同接口
      const res = yield call(api.AttributeAnalysis.getIncome[test], payload);
      if (res && res.code == 0) {
        yield put({
          type: 'save',
          payload: {
            Income: res.data,
          },
        });
      } else {
        yield put({
          type: 'save',
          payload: {
            Income: [],
          },
        });
        message.error(res.msg ? res.msg : '未知错误');
      }
    },

    // 获取超额收益趋势图数据
    *getExcessTrend({ payload }, { call, put }) {
      const test = index(payload.setupModel); // 根据不同模型调不同接口
      const res = yield call(api.AttributeAnalysis.getExcessTrend[test], payload);
      if (res && res.code == 0) {
        yield put({
          type: 'save',
          payload: {
            ExcessTrendData: res.data,
          },
        });
      } else {
        yield put({
          type: 'save',
          payload: {
            ExcessTrendData: { xAxis: [], series: [] },
          },
        });
        message.error(res.msg ? res.msg : '未知错误');
      }
    },
    // 行业权重差&&行业收益贡献差
    *getDiffWeight({ payload }, { call, put }) {
      const res = yield call(api.AttributeAnalysis.getDiffWeight, payload);
      if (res && res.code == 0) {
        yield put({
          type: 'save',
          payload: {
            diffData: res.data,
          },
        });
      } else {
        yield put({
          type: 'save',
          payload: {
            diffData: { series: [], series2: [], yAxis: [] },
          },
        });
        message.error(res.msg ? res.msg : '未知错误');
      }
    },
    // 归因明细
    *getAttributeInfo({ payload }, { call, put }) {
      const test = index(payload.setupModel); // 根据不同模型调不同接口
      const res = yield call(api.AttributeAnalysis.getAttributeInfo[test], payload);
      if (res && res.code == 0) {
        yield put({
          type: 'save',
          payload: {
            info: res.data,
          },
        });
      } else {
        message.error(res.msg ? res.msg : '未知错误');
      }
    },

    // 按修正久期
    *getRevisionPeriod({ payload }, { call, put }) {
      const res = yield call(api.AttributeAnalysis.getRevisionPeriod, payload);
      if (res && res.code == 0) {
        let data = res.data.series;
        let arr0 = [];
        let arr1 = [];
        if (data && data.length) {
          data.map((item) => {
            if (item.name == '资产占比') {
              item.yAxisIndex = 1;
              item.type = 'line';
              arr1 = arr1.concat(item.data);
            } else {
              arr0 = arr0.concat(item.data);
            }
          });
        }
        res.data.yAxisRange = calcRange(arr0, arr1);
        yield put({
          type: 'save',
          payload: {
            RevisionPeriod: res.data,
          },
        });
      } else {
        yield put({
          type: 'save',
          payload: {
            RevisionPeriod: { xAxis: [], series: [] },
          },
        });
        message.error(res.msg ? res.msg : '未知错误');
      }
    },

    // 按债券类别
    *getBondType({ payload }, { call, put }) {
      const res = yield call(api.AttributeAnalysis.getRevisionPeriod, payload);
      if (res && res.code == 0) {
        let data = res.data.series;
        let arr0 = [];
        let arr1 = [];
        if (data && data.length) {
          data.map((item) => {
            if (item.name == '资产占比') {
              item.yAxisIndex = 1;
              item.type = 'line';
              arr1 = arr1.concat(item.data);
            } else {
              arr0 = arr0.concat(item.data);
            }
          });
        }
        res.data.yAxisRange = calcRange(arr0, arr1);
        yield put({
          type: 'save',
          payload: {
            BondType: res.data,
          },
        });
        // callback(res.data)
      } else {
        yield put({
          type: 'save',
          payload: {
            BondType: { xAxis: [], series: [] },
          },
        });
        message.error(res.msg ? res.msg : '未知错误');
      }
    },

    // 因子收益率
    *getFactorYield({ payload }, { call, put }) {
      const res = yield call(api.AttributeAnalysis.getFactorYield, payload);
      if (res && res.code == 0) {
        yield put({
          type: 'save',
          payload: {
            FactorYield: res.data,
          },
        });
      } else {
        yield put({
          type: 'save',
          payload: {
            FactorYield: { xAxis: undefined, series: [] },
          },
        });
        message.error(res.msg ? res.msg : '未知错误');
      }
    },

    // 因子暴露度
    *getFactorExposure({ payload, callback }, { call, put }) {
      const res = yield call(api.AttributeAnalysis.getFactorExposure, payload);
      if (res && res.code == 0) {
        callback(res.data);
      } else {
        message.error(res.msg ? res.msg : '未知错误');
      }
    },

    // 因子收益率
    *benchData({ payload, callback }, { call, put }) {
      const res = yield call(api.AttributeAnalysis.getBenchData, payload);
      if (res && res.code == 0) {
        callback(true);
      } else {
        message.error(res.msg);
      }
    },
    // 因子收益贡献
    *getFactorContributionDetail({ payload, callback }, { call, put }) {
      const res = yield call(api.AttributeAnalysis.getFactorContributionDetail, payload);
      if (res && res.code == 0) {
        callback(res);
      } else {
        message.error(res.msg);
      }
    },
  },

  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
